package com.lp.learn.learnspringboot.redis;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.concurrent.TimeUnit;

public abstract class AbstractRedisService<T> {

    private static Logger logger = LoggerFactory.getLogger(AbstractRedisService.class);

    protected boolean set(RedisTemplate redisTemplate,String key,T value){
        boolean flag = false;
        try {
            redisTemplate.opsForValue().set(key,value);
            flag = true;
        } catch (Exception e) {
            logger.error("redisService set error",e);
        }
        return flag;
    }

    protected boolean set(RedisTemplate redisTemplate,String key,T value,int timeout,TimeUnit timeUnit){
        boolean flag = false;
        try {
            set(redisTemplate,key,value);
            redisTemplate.expire(key,10,timeUnit);
            flag = true;
        } catch (Exception e) {
            logger.error("redisService set error",e);
        }
        return flag;
    }
}
